package com.app.web.sys.phone;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.app.utils.base.http.ResultUtil;
import com.app.web.sys.phone.model.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Random;

/**
 * @program: tbyy-app->PhoneController
 * @Auther: 王伟
 * @Date: 2019/11/7 17:21
 * @Description: 短信及电话信息
 */
@Controller
@RequestMapping("/system/phone/")
@EnableAutoConfiguration
public class PhoneController {

    @Autowired
    private PhoneService phoneService;
    @RequestMapping("sendSMS")
    @ResponseBody
    @SuppressWarnings("all")
    public String sendSMS(String phone){
        try{
            //查询短信信息
            PhoneCode phoneCode=phoneService.selectByPrimaryKey(phone);
            if(phoneCode!=null){
                Date lastSendTime=phoneCode.getSendTime();
                if(new Date().getTime()/1000-lastSendTime.getTime()/1000<60){
                    return ResultUtil.error(6001,"短信获取太频繁，请稍后再试");
                }
                if(phoneCode.getTotoalCount()>100){
                    return ResultUtil.error(6001,"短信超过限制，请联系客服");
                }
            }
            Integer code=new Random().nextInt(9000)+1000;
            PhoneCode phoneC=new PhoneCode();
            phoneC.setCode(code+"");
            phoneC.setPhone(phone);
            phoneService.addPhoneCode(phoneC);
            //发送短信
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FgWeV6KTeHYjxoqGrbn", "OhCKIZpA2UxpcGqzR9mJbXUJRt2wf1");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest req = new CommonRequest();
            req.setMethod(MethodType.POST);
            req.setDomain("dysmsapi.aliyuncs.com");
            req.setVersion("2017-05-25");
            req.setAction("SendSms");
            req.putQueryParameter("RegionId", "cn-hangzhou");
            req.putQueryParameter("PhoneNumbers", phone);
            req.putQueryParameter("SignName", "淘美美");
            req.putQueryParameter("TemplateCode", "SMS_174273456");
            req.putQueryParameter("TemplateParam", "{'code':'"+code+"'}");
            try {
                CommonResponse response = client.getCommonResponse(req);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }

        }catch (Exception e){
            return ResultUtil.error(6001,"发送失败");
        }
        return ResultUtil.success();
    }
}
